package com.clgc.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.clgc.baseservice.exception.MyException;
import com.clgc.eduservice.entity.EduTeacher;
import com.clgc.eduservice.entity.excel.TeacherExcel;
import com.clgc.eduservice.service.EduTeacherService;
import com.clgc.eduservice.service.impl.EduTeacherServiceImpl;
import com.clgc.utils.MD5Utils;
import com.clgc.utils.ResultCode;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class TeacherListener extends AnalysisEventListener<TeacherExcel> {

    @Autowired
    private EduTeacherService eduTeacherService;
//
//    public TeacherListener(EduTeacherService eduTeacherService) {
//        this.eduTeacherService = eduTeacherService;
//    }

    @Override
    public void invoke(TeacherExcel teacherExcel, AnalysisContext analysisContext) {
        System.out.println("----------------读取excel表---------------");
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setUsername(teacherExcel.getUsername());
        eduTeacher.setName(teacherExcel.getName());

        //md5加密
        String pwd = teacherExcel.getPwd();
        String md5Pwd = MD5Utils.MD5Encode(pwd);

        eduTeacher.setPwd(md5Pwd);
        eduTeacher.setSex(teacherExcel.getSex());
        System.out.println(eduTeacher);

        //向数据库写入
        eduTeacherService.save(eduTeacher);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
